using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
using System.IO;
#endif

public class SpawnObject : MonoBehaviour
{
	public InteractiveType type;
	public string toSpawn;

	private void Start()
	{
		InteractiveInfo info = InteractiveManager.Instance.GetInfo(toSpawn);
		if (info != null)
		{
			FloorManager.Instance.SpawnInteractive(info, transform);
		}
		else
		{
			Debug.LogError("Error - Object to spawn was null");
		}

		DestroyImmediate(gameObject);
	}

	private void OnDrawGizmos()
	{
		Gizmos.DrawIcon(transform.position, "Spawn/gizmo" + type.ToString());
	}
}

#if UNITY_EDITOR
[CustomEditor(typeof(SpawnObject))]
public class SpawnObjectEditor : Editor
{
	private SpawnObject spawn;
	private Dictionary<InteractiveType, List<InteractiveInfo>> interactives = new Dictionary<InteractiveType, List<InteractiveInfo>>();

	private void OnEnable()
	{
		spawn = target as SpawnObject;
		InteractiveManager.LoadAssetDictionary(ref interactives);
		LoadAssets();
	}

	public override void OnInspectorGUI ()
	{
		EditorGUILayout.BeginHorizontal();
		{
			float width = (float)Screen.width / (float)((int)InteractiveType.COUNT + 1);
			for (int i = 0; i < (int)InteractiveType.COUNT; i++)
			{
				TileSprite((InteractiveType)i, width);
			}
		}
		EditorGUILayout.EndHorizontal();

		List<string> names;
		if (interactives.ContainsKey(spawn.type))
		{
			names = AssetNames(interactives[spawn.type]);
			int n = EditorGUILayout.Popup(names.IndexOf(spawn.toSpawn), names.ToArray());
			if (n != -1)
			{
				spawn.toSpawn = names[n];
				spawn.name = "Spawn_" + spawn.type.ToString() + "_" + spawn.toSpawn;
			}
		}
		else
		{
			EditorGUILayout.LabelField("Corresponding type has no assets saved");
		}

		if (GUI.changed)
		{
			EditorUtility.SetDirty(spawn);
		}
	}
	
	private void TileSprite(InteractiveType _type, float _width)
	{
		if (spawn.type == _type) GUI.color = Color.grey;
		
		string path = "Assets/Gizmos/Spawn/gizmo" + _type.ToString() + ".png";
		Texture img = (Texture)AssetDatabase.LoadAssetAtPath(path, typeof(Texture));
		if (GUILayout.Button(img, GUILayout.Width(_width), GUILayout.Height(_width)))
		{
			spawn.type = _type;
		}
		
		GUI.color = Color.white;
	}

	private void LoadAssets()
	{
		for (int i = 0; i < (int)InteractiveType.COUNT; i++)
		{
			string folder = "Assets/Resources/Stats/" + ((InteractiveType)i).ToString() + "s";
			if (Directory.Exists(folder))
			{
				string[] objs = AssetDatabase.FindAssets("", new string[] {folder});
				foreach (string guid in objs)
				{
					string path = AssetDatabase.GUIDToAssetPath(guid);
					InteractiveInfo info = AssetDatabase.LoadAssetAtPath(path, typeof(InteractiveInfo)) as InteractiveInfo;
					if (info)
					{
						if (!interactives.ContainsKey((InteractiveType)i))
						{
							interactives.Add((InteractiveType)i, new List<InteractiveInfo>());
						}
						
						if (!interactives[(InteractiveType)i].Contains(info))
						{
							interactives[(InteractiveType)i].Add(info);
						}
					}
				}
			}
		}
	}

	private List<string> AssetNames(List<InteractiveInfo> _assets)
	{
		List<string> names = new List<string>();
		foreach (InteractiveInfo info in _assets)
		{
			names.Add(info.itemName);
		}

		return names;
	}
}
#endif